<template>
  <div class="comment-box">
    <h2>关于{{ menuInfo.title }}的讨论</h2>

    <div v-if="!commentList.length" class="comment-empty">
      暂无讨论~
    </div>

    <div class="comment-text" v-if="isLogin">
      <img :src="userInfo.avatar" alt="" class="user-avatar" />

      <div class="comment-right">
        <el-input
          type="textarea"
          :rows="5"
          :cols="50"
          placeholder="请输入内容~"
          v-model="comment"
        />
        <div class="comment-button">
          <el-button
            class="send-comment"
            type="primary"
            size="medium"
            @click="submit"
          >
            提交
          </el-button>
        </div>
      </div>
    </div>

    <div class="comment-list-box">
      <ul class="comment-list">
        <li v-for="item in commentList" :key="item.commentId">
          <router-link
            :to="{ name: 'space', query: { userId: item.userId } }"
            class="avatar"
          >
            <img :src="item.userInfo.avatar" alt="" />
            <h5>{{ item.userInfo.name }}</h5>
          </router-link>

          <div class="comment-detail clearfix">
            <p>{{ item.commentText }}</p>
            <div class="info">{{ item.createdAt }}</div>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { getComments, postComments } from "@/service";
import { mapGetters } from "vuex";

export default {
  props: {
    menuInfo: {
      type: Object,
      default: () => ({}),
    },
  },

  computed: {
    ...mapGetters(["isLogin"]),
    userInfo() {
      return this.$store.state.userInfo;
    },
  },

  data() {
    return {
      comment: "",
      commentList: [],
    };
  },

  watch: {
    menuInfo: {
      async handler() {
        this.commentList = (
          await getComments({ menuId: this.menuInfo.menuId })
        ).data.comments;
      },
    },
  },

  methods: {
    submit() {
      if (!this.comment) {
        this.$message({
          message: "评论信息不能为空！",
          type: "warning",
        });
        return;
      }
      postComments({
        menuId: this.menuInfo.menuId,
        commentText: this.comment,
      }).then((res) => {
        if (res.ec === 200) {
          this.$message({
            message: "已提交评论！",
            type: "success",
          });
          this.comment = "";
          this.commentList.unshift(res.data.comments);
        }
      });
    },
  },
};
</script>

<style>
.comment-box {
  background-color: #fff;
  margin-top: 20px;
  padding: 0 20px;
}
h2 {
  font-size: 24px;
  color: #333;
  height: 66px;
  line-height: 66px;
  border-bottom: 1px solid #eee;
}
.comment-text {
  margin-top: 20px;
  border-bottom: 1px solid #eee;
}
.user-avatar {
  margin-right: 20px;
  vertical-align: top;
  width: 36px;
  height: 36px;
}

.comment-right {
  display: inline-block;
  width: 80%;
}
.comment-button {
  text-align: right;
  margin-top: 10px;
}
.comment-button .send-comment {
  background-color: #ff3232;
  border: none;
}
.comment-list-box {
  margin-top: 20px;
  padding-top: 30px;
}
.comment-list-box ul li {
  border-bottom: 1px solid #eee;
  margin-bottom: 20px;
}
.comment-list-box li .avatar {
  height: 82px;
  width: 50px;
  overflow: hidden;
  display: inline-block;
}
.avatar img {
  height: 50px;
  width: 50px;
}
.avatar h5 {
  white-space: nowrap;
  text-align: center;
}
.comment-detail {
  display: inline-block;
  vertical-align: top;
  margin-left: 20px;
  width: 80%;
}
.comment-detail p {
  margin: 0;
  font-size: 14px;
}
.comment-detail .info {
  margin-top: 10px;
  color: #999;
  padding: 0;
}
.comment-empty{
  font-size: 18px;
  text-align: center;
  color: #999;
  margin: 20px auto;
}
</style>